﻿using System;
using System.Collections.Generic;

namespace Core
{
    public class AssetSpace : IDisposable
    {
        Dictionary<string, int> assetPathCount = new Dictionary<string, int>();

        public async ETTask<T> LoadAssetAsync<T>(string path) where T : UnityEngine.Object
        {
            AddAssetReference(path);
            var data = await new LoadAssetAsync<T>(path).PublishAsync();
            return data.asset;
        }

        public void AddAssetReference(string path)
        {
            if (assetPathCount.ContainsKey(path))
            {
                assetPathCount[path]++;
            }
            else
            {
                assetPathCount[path] = 1;
            }
        }

        public void Dispose()
        {
            foreach (var pair in assetPathCount)
                new UnloadAsset(pair.Key, pair.Value).Publish();

            assetPathCount.Clear();
        }
    }
}
